<?php
class TradeItemService {
    private static $instance;

    public static function get() {
        if (!self::$instance) {
            self::$instance = new TradeItemService();
        }
        return self::$instance;
    }

    protected function __construct() {
    }

    /**
     * 初始化
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function refresh($userId,$reqTime) {
        //获取交易模块配置
        $tradeGridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, null, true);
        if (!$tradeGridConf || empty($tradeGridConf)){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $total = 0;
        $resourceArr = array();
        if ($tradeItemList->getListCount() != 0) {//进行更新数据
//            $todayTime = strtotime(date('Y-m-d', SzSystem::getSysTime()));
            $todayTime = strtotime(date('Y-m-d', $reqTime));
            foreach ($tradeItemList->getList() as $position=>$data){
                $tradeItem = $tradeItemList->getElement($position);
                $state = $tradeItem->getState();

                $num = $tradeItem->getNum();
                $price = $tradeItem->getPrice();
                if ($state == 3){//已出售
                    $tradeItem->setNum(0);
                    $tradeItem->setPrice(0);
                    $tradeItem->setItemId(0);
                    $tradeItem->setState(1);
                    $total = $total + $num*$price;
                }

                if ($state == 0){
                    continue;
                }

                //每日更新
                $overDay = $tradeItem->getUpdateTime();
                $times = $tradeItem->getTimes();
                if ($overDay < $todayTime){// && $times < $tradeGridConf[$position]['putawayTimes']
                    $tradeItem->setUpdateTime($reqTime);
                    $tradeItem->setTimes($tradeGridConf[$position]['putawayTimes']);
                    $tradeItem->setBuyShelf(0);
                }

                //过期更新
                $oldExpirationTime = $tradeItem->getShelfTime();
                if ($oldExpirationTime != 0){
                    $newExpirationTime = $oldExpirationTime + 7*86400;
                    if ($reqTime > $newExpirationTime){
                        //返还玩家资源
                        if ($state == 2){
                            $itemId = $tradeItem->getItemId();
                            $num = $tradeItem->getNum();
                            $entityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET, GameConst::ENTITY_TYPE_ITEM, $itemId);
                            GameService::get()->handleEntity($userId, $entityId, $num, true);
                            CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,1,$entityId.':'.$num.'|','refresh');//消耗资源上报

                            if (!empty($resourceArr[$itemId])){
                                $resourceArr[$itemId] = $resourceArr[$itemId] + $num;
                            }
                            else{
                                $resourceArr[$itemId] = $num;
                            }

                            $tradeItem->setState(1);
                            $tradeItem->setTimes($tradeGridConf[$position]['putawayTimes']);
                            $tradeItem->setBuyShelf(0);
                            $tradeItem->setPrice(0);
                            $tradeItem->setShelfTime($reqTime);
                            $tradeItem->setItemId(0);
                            $tradeItem->setNum(0);
                            $tradeItem->setUpdateTime($reqTime);
                        }
                    }
                }

                $tradeItemList->updateElement($position,$tradeItem);
            }
        }
        else{//进行创建数据
            foreach ($tradeGridConf as $pos=>$conf){
                $tradeItem = $tradeItemList->getElement($pos,false);
                if (!$tradeItem) {
                    $tradeItem = new TradeItemVo(
                        $userId, // userId
                        $pos, //position
                        $conf['type'] == 0 ? 1 : 0, // state
                        $conf['putawayTimes'], // times
                        0,//shelfTime
                        0,//buyshelf
                        0,//price
                        0,//share
                        0,//item
                        0,//num
                        $reqTime,//create
                        $reqTime,//update
                        true
                    );
                }

                if ($tradeItem->isInsert() && !SzUtility::checkArrayKey($pos, $tradeItemList->getList())) {
                    $tradeItemList->addElement($tradeItem);
                    $tradeItemList->setElement($pos,$tradeItem);
                }
            }
        }

        if ($total){
            //如果有玩家购买了我的商品，我自己加金币
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_TOKEN, $total, true);
            CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,1,GameConst::ENTITY_ACTION_GET_TOKEN.':'.$total.'|','refresh');//消耗资源上报

        }

        SzPersister::get()->setVoList($tradeItemList);

//        return $tradeItemList->toArray();
        return array(
            'shop' => SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM)->toArray(),
            'ret' => $resourceArr
        );
    }

    /**
     * 店铺明细
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function info($userId,$reqTime) {
        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemArr = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM)->toArray();

        if (empty($tradeItemArr)){
            return array();
        }

        foreach ($tradeItemArr as $position=>$data){
            unset($data['userId']);
            unset($data['times']);
            unset($data['buyShelf']);
            unset($data['shareTimes']);
            unset($data['createTime']);
            unset($data['updateTime']);
            $tradeItemArr[$position] = $data;
        }

        return $tradeItemArr;
    }

    /**
     * 获取店铺格子
     * @param $userId
     * @param $position
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function getBox($userId,$position,$reqTime) {
        //获取交易模块配置
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        if (!$gridConf || empty($gridConf)){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $tradeItem = $tradeItemList->getElement($position,false);
        if (!$tradeItem){
            throw new SzException(90086);//请先购买格子
        }

        $state = $tradeItem->getState();
        if ($state){
            throw new SzException(90087);//格子已经购买
        }

        //获取玩家平台数据
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);

        //格子购买配置
        $type = $gridConf['type'];
        if ($type == 1){
            //消耗道具购买
            list($use, $num) = (reset($gridConf['unlockConsume']) ? each($gridConf['unlockConsume']) : each($gridConf['unlockConsume']));
            GameService::get()->handleEntity($userId,$use, $num, true);
            CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,0,GameConst::ENTITY_ACTION_CONSUME_MONEY.':'.$num.'|','getBox');//消耗资源上报
            //获得格子
            $tradeItem->setState(1);
            $tradeItem->setUpdateTime($reqTime);

            $tradeItemList->updateElement($position,$tradeItem);
            SzPersister::get()->setVoList($tradeItemList);
        }
        elseif ($type == 16001){
            if (in_array($platform,GameConst::PLATFORM_NOTSHARE_ARRAY)){
                //消耗道具购买
                list($oppoUse, $oppoNum) = (reset($gridConf['unlockConsume']) ? each($gridConf['unlockConsume']) : each($gridConf['unlockConsume']));
                GameService::get()->handleEntity($userId,$oppoUse, $oppoNum, true);

                //获得格子
                $tradeItem->setState(1);
                $tradeItem->setUpdateTime($reqTime);

                $tradeItemList->updateElement($position,$tradeItem);
                SzPersister::get()->setVoList($tradeItemList);
            }
            else{
                //验证分享次数
                $share = $tradeItem->getShareTimes();
                if ($share < $gridConf['helpNumber']){
                    throw new SzException(90088);//分享次数不足
                }
                else{
                    $tradeItem->setState(1);
                    $tradeItem->setUpdateTime($reqTime);

                    $tradeItemList->updateElement($position,$tradeItem);
                    SzPersister::get()->setVoList($tradeItemList);
                }
            }
        }
        else{
            throw new SzException(90089);//格子免费无需购买
        }

        return true;
    }

    /**
     * 记录分享次数
     * @param $userId
     * @param $position
     * @param $times
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function setShareTimes($userId,$position,$times,$reqTime) {
        $times = empty($times) ? 1 : $times;
        //获取交易模块配置
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        if (!$gridConf || empty($gridConf) || !$gridConf['helpNumber'] || empty($gridConf['helpNumber'])){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $tradeItem = $tradeItemList->getElement($position,false);
        if (!$tradeItem){
            throw new SzException(90086);//请先购买格子
        }

        $shareTimes = $tradeItem->getShareTimes();
        $tradeItem->setShareTimes($shareTimes + $times);

        $tradeItemList->updateElement($position,$tradeItem);
        SzPersister::get()->setVoList($tradeItemList);

        return true;
    }

    /**
     * 上架
     * @param $userId
     * @param $position
     * @param $itemId
     * @param $num
     * @param $price
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function upperShelf($userId,$position,$itemId,$num,$price,$reqTime) {
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        $tradeItemsConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_ITEMS, null, true);
        if (!$gridConf || empty($gridConf)){
            return array(
                'code' => 90082,//配置不存在
                'situation' => array()
            );

//            throw new SzException(90082);//配置不存在
        }
        if (!$tradeItemsConf || empty($tradeItemsConf)){
            return array(
                'code' => 90082,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90082);//配置不存在
        }

        $itemConf = array();
        foreach ($tradeItemsConf as $index=>$data){
            if ($data['itemId'] == $itemId){
                $itemConf = $data;
                break;
            }
        }
        if (!$itemConf || empty($itemConf)){
            return array(
                'code' => 90082,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90082);//配置不存在
        }

        if ($num < $itemConf['itemAmount'][0] || $num > $itemConf['itemAmount'][1]){
            return array(
                'code' => 90090,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90090);//商品数量超出设置
        }
        if ($price < $itemConf['unitPrice'][0] || $price > $itemConf['unitPrice'][2]){
            return array(
                'code' => 90091,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90091);//商品单价超出设置
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $tradeItem = $tradeItemList->getElement($position,false);
        if (!$tradeItem){
            return array(
                'code' => 90086,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90086);//请先购买格子
        }

        $state = $tradeItem->getState();
        if ($state == 0){
            return array(
                'code' => 90086,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90086);//请先购买格子
        }
        if ($state == 2){
            return array(
                'code' => 90092,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90092);//商品已经上架
        }
        if ($state == 3){
            return array(
                'code' => 90100,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90100);//商品售出
        }

        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $item = $itemList->getElementsByItemDefId($itemId);
        if (!$item) {
            return array(
                'code' => 90093,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90093);//仓库中没有该商品
        }

        // 获取道具的拥有数量
        $mineNum = $itemList->getItemDefCount($itemId);
        if ($mineNum < $num){
            return array(
                'code' => 90094,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90094);//该商品库存不足
        }
        if ($mineNum - $num < 0){
            return array(
                'code' => 90094,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90094);//该商品库存不足
        }

        $hasTimes = $tradeItem->getTimes();
        if ($hasTimes <= 0){
            return array(
                'code' => 90095,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90095);//请先购买上架次数
        }

        //上架处理
        $tradeItem->setState(2);
        $tradeItem->setTimes($hasTimes - 1);
        $tradeItem->setShelfTime($reqTime);
        $tradeItem->setPrice($price);
        $tradeItem->setItemId($itemId);
        $tradeItem->setNum($num);
        $tradeItem->setUpdateTime($reqTime);

        //暂时扣除玩家仓库道具
        $entityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_CONSUME, GameConst::ENTITY_TYPE_ITEM, $itemId);
        GameService::get()->handleEntity($userId, $entityId, $num, true);
        CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,0,$entityId.':'.$num.'|','upperShelf');//消耗资源上报

        //货架数据更新
        $tradeItemList->updateElement($position,$tradeItem);
        SzPersister::get()->setVoList($tradeItemList);

        return array(
            'code' => 0,
            'situation' => $tradeItem->toArray()
        );
    }

    /**
     * 下架
     * @param $userId
     * @param $position
     * @param $itemId
     * @param $num
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function downShelf($userId,$position,$itemId,$num,$reqTime) {
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        if (!$gridConf || empty($gridConf)){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $tradeItem = $tradeItemList->getElement($position,false);
        if (!$tradeItem){
            throw new SzException(90086);//请先购买格子
        }

        $state = $tradeItem->getState();
        if ($state == 0){
            return array(
                'code' => 90086,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90086);//请先购买格子
        }
        if ($state == 1){
            return array(
                'code' => 90096,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90096);//商品没有上架
        }
        if ($state == 3){
            return array(
                'code' => 90100,//配置不存在
                'situation' => array()
            );
//            throw new SzException(90100);//商品售出
        }

        $itemId = $itemId ? $itemId : $tradeItem->getItemId();
        $num = $num ? $num : $tradeItem->getNum();
        //下架处理
        $tradeItem->setState(1);
        $tradeItem->setShelfTime($reqTime);
        $tradeItem->setPrice(0);
        $tradeItem->setItemId(0);
        $tradeItem->setNum(0);
        $tradeItem->setUpdateTime($reqTime);

        //增加玩家仓库道具
        $entityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET, GameConst::ENTITY_TYPE_ITEM, $itemId);
        GameService::get()->handleEntity($userId, $entityId, $num, true);
        CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,1,$entityId.':'.$num.'|','downShelf');//消耗资源上报

        //货架数据更新
        $tradeItemList->updateElement($position,$tradeItem);
        SzPersister::get()->setVoList($tradeItemList);

        return array(
            'code' => 0,
            'situation' => $tradeItem->toArray()
        );
    }

    /**
     * 购买上架次数
     * @param $userId
     * @param $position
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function buyShelfTimes($userId,$position,$reqTime) {
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        if (!$gridConf || empty($gridConf)){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $tradeItem TradeItemVo
         */
        $tradeItemList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRADE_ITEM);
        $tradeItem = $tradeItemList->getElement($position,false);
        if (!$tradeItem){
            throw new SzException(90086);//请先购买格子
        }

        $state = $tradeItem->getState();
        if ($state == 0){
            throw new SzException(90086);//请先购买格子
        }

        //获取玩家今日该格子的剩余次数
        $leftTimes = $tradeItem->getTimes();
        if ($leftTimes >= 1){
            throw new SzException(90097);//今日可上架次数未清空
        }

        //购买处理
        $buyTimes = $tradeItem->getBuyShelf();
        $todayCanBuy = count($gridConf['diamondConsume']);
        if ($buyTimes + 1 > $todayCanBuy){
            throw new SzException(90098);//今日已达购买上限
        }
        $tradeItem->setTimes(1);
        $tradeItem->setBuyShelf($buyTimes + 1);
        $tradeItem->setUpdateTime($reqTime);

        //进行道具消耗
        $useCash = $gridConf['diamondConsume'][$buyTimes];
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $useCash, true);
        CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,0,GameConst::ENTITY_ACTION_CONSUME_MONEY.':'.$useCash.'|','buyShelfTimes');//消耗资源上报


        //货架数据更新
        $tradeItemList->updateElement($position,$tradeItem);
        SzPersister::get()->setVoList($tradeItemList);

        return true;
    }

    /**
     * 交易
     * @param $userId
     * @param $targetId
     * @param $position
     * @param $upperTime
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function trade($userId,$targetId,$position,$upperTime,$reqTime) {
        $gridConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_FRIENDS_DEAL_GRID, $position, true);
        if (!$gridConf || empty($gridConf)){
            throw new SzException(90082);//配置不存在
        }

        /**
         * 获取目标玩家数据
         * @var $tradeItemList TradeItemVoList
         * @var $targetTradeItem TradeItemVo
         */
        $targetTradeItemList = SzPersister::get()->getVoList($targetId, GameConst::ORM_NAME_TRADE_ITEM);
        $targetTradeItem = $targetTradeItemList->getElement($position,false);
        if (!$targetTradeItem){
            return array(
                'code' => 90099,//目标玩家没有该商品
                'buy' => false
            );
//            throw new SzException(90099);
        }

        $state = $targetTradeItem->getState();
        if ($state == 0 || $state == 1 || $state == 3){
            return array(
                'code' => 90099,//目标玩家没有该商品
                'buy' => false
            );
//            throw new SzException(90099);//目标玩家没有该商品
        }

        $newUpperTime = $targetTradeItem->getShelfTime();
        if ($newUpperTime != $upperTime){
            return array(
                'code' => 90099,//目标玩家没有该商品
                'buy' => false
            );
//            throw new SzException(90099);//目标玩家没有该商品
        }

        //买家处理
        $price = $targetTradeItem->getPrice();
        $num = $targetTradeItem->getNum();
        $itemId = $targetTradeItem->getItemId();
        $total = $price*$num;
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_TOKEN, $total, true);
        CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,0,GameConst::ENTITY_ACTION_CONSUME_TOKEN.':'.$total.'|','trade');//消耗资源上报
        $entityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET, GameConst::ENTITY_TYPE_ITEM, $itemId);
        GameService::get()->handleEntity($userId, $entityId, $num, true);
        CommonFunctionService::get()->reportResourceUseDetails(17000076,$userId,1,$entityId.':'.$num.'|','trade');//消耗资源上报

        //卖家处理
        $targetTradeItem->setState(3);
//        $targetTradeItem->setItemId(0);
//        $targetTradeItem->setNum(0);
//        $targetTradeItem->setPrice(0);
        $targetTradeItem->setUpdateTime($reqTime);

//        GameService::get()->handleEntity($targetId, GameConst::ENTITY_ACTION_GET_TOKEN, $total, true);

        //货架数据更新
        $targetTradeItemList->updateElement($position,$targetTradeItem);
        SzPersister::get()->setVoList($targetTradeItemList);

        //交易记录
        $reportMessage = array(
            'seller' => $targetId,
            'buyer' => $userId,
            'item' => $itemId,
            'num' => $num,
            'price' => $price,
            'tradeTime' => $reqTime
        );
        $redisDb = SzDbFactory::get()->getDb($targetId, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->lTrim(GameConst::CACHE_KEY_TRADE_SHOP_MESSAGE_LIST.$targetId,0,99);
        $redisDb->lPush(GameConst::CACHE_KEY_TRADE_SHOP_MESSAGE_LIST.$targetId,json_encode($reportMessage,true));

        return array(
            'code' => 0,
            'buy' => true,
        );
    }

    /**
     * 获取玩家好友交易状态
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function getFriendsTradeShop($userId) {
        //获取玩家所有好友
        $friends = FriendService::get()->getFriendInfoList($userId);
        $friendsShopSituation = array();

        if (empty($friends)){
            return array();
        }

        foreach ($friends as $fid=>$data){
            /**
             * 获取玩家好友数据
             * @var $friendTradeItemList TradeItemVoList
             */
            $friendTradeItemList = SzPersister::get()->getVoList($fid, GameConst::ORM_NAME_TRADE_ITEM);
            if ($friendTradeItemList->getListCount() != 0){
                $temp = $this->arraySort($friendTradeItemList->toArray(), 'shelfTime', SORT_DESC);
                $newTime = reset($temp['data'])['shelfTime'];
                if (count($temp['data']) == 0){
                    continue;
                }

                if ($temp['num'] > 0){
                    $friendsShopSituation[$fid]['newTime'] = $newTime ? $newTime : 0;
                    $friendsShopSituation[$fid]['situation'] = !empty($temp['data']) ? $temp['data'] : array();
                    $friendsShopSituation[$fid]['other'] = ActivityService::get()->getUserInfo($fid,0);
                }
            }
        }

        return $friendsShopSituation;
    }

    /**
     * 二维数组根据某个字段排序
     * @param array $array 要排序的数组
     * @param string $keys   要排序的键字段
     * @param string $sort  排序类型  SORT_ASC     SORT_DESC
     * @return array 排序后的数组
     */
    function arraySort($array, $keys, $sort = SORT_DESC) {
        $hasNum = 0;
        $keysValue = [];
        foreach ($array as $k => $v) {
                unset($v['userId']);
                unset($v['times']);
                unset($v['buyShelf']);
                unset($v['shareTimes']);
                unset($v['createTime']);
                unset($v['updateTime']);
                $array[$k] = $v;
                $keysValue[$k] = $v[$keys];
                if ($v['itemId'] && $v['state'] == 2){
                    $hasNum++;
                }
        }

        array_multisort($keysValue, $sort, $array);
        return array(
            'data' => $array,
            'num' => $hasNum
            );
    }

    /**
     * 消息列表
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getMessage($userId,$reqTime) {
        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
        $list = $redisDb->lRange(GameConst::CACHE_KEY_TRADE_SHOP_MESSAGE_LIST.$userId,0,-1);

        $data = array();
        if ($list) {
            foreach ($list as $key => $val) {
                $val = json_decode($val,true);
                if ($val['tradeTime'] < ($reqTime - GameConst::SOCIAL_VALID_TIME)) {
                    continue;
                }

                /** @var ModuleProfileVo $profile */
                $profile = SzPersister::get()->getVo($val['buyer'], ModuleProfileConst::ORM_NAME_PROFILE);
                if(!$profile) {
                    continue;
                };

                $data[$key]['seller'] = $val['seller'];
                $data[$key]['buyer'] = $val['buyer'];
                $data[$key]['item'] = $val['item'];
                $data[$key]['num'] = $val['num'];
                $data[$key]['price'] = $val['price'];
                $data[$key]['tradeTime'] = $val['tradeTime'];
                $data[$key]['buyerData'] = ActivityService::get()->getUserInfo($val['buyer'],0);
            }
        }

        return array(
            'data' => $data,
        );
    }
}